package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AdminEditUser;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.helpers.AdminParticipantTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.gui.dialogs.OptionDialog;
import eu.livotov.tpt.gui.dialogs.OptionKind;
import eu.livotov.tpt.i18n.TM;

/**
 * Edit participants
 *
 */
@SuppressWarnings("serial")
public class AdminTab3 extends VerticalLayout {
	
	Admin admin;
	UserModel userModel;
	Table participantsTable;
	VerticalLayout tableWrap;
	
	public AdminTab3(Admin admin) {
		this.admin = admin;
		userModel = new UserModel();
		buildLayout();
	}

	public void buildLayout() {
		VerticalLayout wrap = new VerticalLayout();
		wrap.setSpacing(true);
		wrap.setMargin(true);
		
		tableWrap = new VerticalLayout();
		tableWrap.addComponent(buildTable());
		
		Button refreshTable = new Button(TM.get("action.refresh"));
		refreshTable.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				tableWrap.removeAllComponents();
				tableWrap.addComponent(buildTable());
			}
		});
		refreshTable.setIcon(Config.ICON_REFRESH);
		
		wrap.addComponent(refreshTable);
		wrap.addComponent(tableWrap);
		addComponent(wrap);
	}
	
	public Table buildTable() {
		participantsTable = new Table();
		
		// properties
		participantsTable = new Table();
		participantsTable.setWidth("100%");
		participantsTable.setPageLength(10);
		participantsTable.setSelectable(true);
		
		// data
		final BeanItemContainer<AdminParticipantTableArgs> tableContainer;
		tableContainer = new BeanItemContainer<AdminParticipantTableArgs>(AdminParticipantTableArgs.class);
		
		List<User> users = userModel.getUsers();
		
		for(User o : users) {
			String role = userModel.getRoleText(o.getRole(), admin.userSession.getLanguage()).getTitle();
			String language = "";
			if (o.getLanguage() != null) {
				 language = o.getLanguage().getLocale();
			}
		
			
			String date = o.getRegdate().toString();
			
			AdminParticipantTableArgs pta = new AdminParticipantTableArgs(o, o.getFirstname(), o.getLastname(), o.getEmail(), role, language, date);
			tableContainer.addBean(pta);
		}
		
		// generate buttons
		participantsTable.addGeneratedColumn("gen_edit", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final User thisUser = (User) source.getContainerProperty(itemId, "user").getValue();
				Button b = new Button(TM.get("action.edit"));
				b.setData(thisUser.getId());
				b.addListener(new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						admin.openModal(new AdminEditUser(thisUser.getId()));
					}
				});	
				b.setIcon(Config.ICON_EDIT);
				return b;
			}
		});
		
		participantsTable.addGeneratedColumn("gen_delete", new Table.ColumnGenerator() {
			public Component generateCell(Table source, final Object itemId, Object columnId) {
				final User thisUser = (User) source.getContainerProperty(itemId, "user").getValue();
				Button b = new Button(TM.get("action.delete"));
				b.setData(thisUser.getId());
				b.addListener(new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {

						final OptionDialog dlg = new OptionDialog (TPTApplication.getCurrentApplication ());
						final String name;
						if(thisUser.getFirstname().equals("")) {
							name = thisUser.getEmail();
						} else {
							name = thisUser.getFirstname() + " " + thisUser.getLastname();
						}
						dlg.showConfirmationDialog(TM.get("admin.tab3.userdelete"), TM.get("admin.tab3.userdelete.desc", name), new OptionDialog.OptionDialogResultListener () {
						  public void dialogClosed(OptionKind closeEvent) {
							  if(closeEvent == OptionKind.OK) {
								  NotificationLib.showSuccess(name + " " + TM.get("action.deleted"), null);
								  tableContainer.removeItem(itemId);
								  admin.adminModel.deleteUser(thisUser);
							  }
						  }
						});
						
					}
				});	
				b.setIcon(Config.ICON_DELETE);
				return b;
			}
		});
		
		participantsTable.setContainerDataSource(tableContainer);
		
		participantsTable.setColumnHeader("firstname", TM.get("user.firstname"));
		participantsTable.setColumnHeader("lastname", TM.get("user.lastname"));
		participantsTable.setColumnHeader("email", TM.get("user.email"));
		participantsTable.setColumnHeader("role", TM.get("user.role"));
		participantsTable.setColumnHeader("language", TM.get("user.language"));
		participantsTable.setColumnHeader("gen_edit", TM.get("action.edit"));
		participantsTable.setColumnHeader("gen_delete", TM.get("action.delete"));
		
		participantsTable.setVisibleColumns(
				new Object[]{
						"firstname", 
						"lastname", 
						"email", 
						"language", 
						"gen_edit", 
						"gen_delete"
				}
		);
		
		return participantsTable;
	}
	
}
